	var bkP = null;
	
	
	
	function findFGSinstance()
	{
	
		var wm = Components.classes['@mozilla.org/appshell/window-mediator;1']
				.getService(Components.interfaces.nsIWindowMediator);
		var browserEnumerator = wm.getEnumerator("navigator:browser");
		
		var found = false;
		
		while (!found && browserEnumerator.hasMoreElements())
		{
			var bE = browserEnumerator.getNext();
			if(bE.FGScheck != undefined)
			{
				found = true;
				break;
			}
		}
		
		if(!found || !bE.FGS.alreadyOpened)
		{
			window.location.href = 'about:blank';
		}
		else
		{
			bkP = bE.FGS;
			return;
		}
	}
	findFGSinstance();
	
	function loadNeighborsStats(gameID, data)
	{
		var stmt = bkP.database.db.createStatement("SELECT * FROM neighborStats where gameID = :gameID");
		stmt.params.gameID = gameID;

		var game = bkP.gamesData[gameID].systemName;
		var opt = 'SendFreeGiftsList';
		
		stmt.executeAsync({
			handleResult: function(aResultSet)
			{
				for (var row = aResultSet.getNextRow();	 row;	 row = aResultSet.getNextRow()) 
				{
					var v = 
					{
						userID: 			row.getResultByIndex(0),
						gameID: 		row.getResultByIndex(1),
						lastBonus:			row.getResultByIndex(2),
						lastGift:			row.getResultByIndex(3),
						totalBonuses:			row.getResultByIndex(4),
						totalGifts:			row.getResultByIndex(5),
						lastGiftSent:			row.getResultByIndex(6),
						totalGiftsSent:			row.getResultByIndex(7),
					}

					var arrIndNor = data.nor[v.userID];
					var arrIndFav = data.fav[v.userID];
					
					if(typeof(arrIndFav) != 'undefined')
					{
						var arrInd = arrIndFav;
						var array  = 'favArr';
					}
					else if(typeof(arrIndNor) != 'undefined')
					{
						var arrInd = arrIndNor;
						var array  = 'norArr';
					}
					else
					{
						var arrInd = undefined;
					}
					
					if(typeof(arrInd) != 'undefined')
					{
						var lastBonus 		= (v.lastBonus == 0 ? 		bkP.getMsg('Never') : format_time_ago(v.lastBonus));
						var lastGift  		= (v.lastGift == 0 ? 		bkP.getMsg('Never') : format_time_ago(v.lastGift));
						var lastGiftSent  	= (v.lastGiftSent == 0 ? 	bkP.getMsg('Never') : format_time_ago(v.lastGiftSent));						
						
						data[array][arrInd][2] = '<span class="hide">'+v.lastBonus+'</span>'+lastBonus;
						data[array][arrInd][3] = '<span class="hide">'+v.lastGift+'</span>'+lastGift;
						data[array][arrInd][4] = '<span class="hide">'+v.lastGiftSent+'</span>'+lastGiftSent;
						data[array][arrInd][5] = '<span class="hide">'+v.totalBonuses+'</span>'+v.totalBonuses;
						data[array][arrInd][6] = '<span class="hide">'+v.totalGifts+'</span>'+v.totalGifts;
						data[array][arrInd][7] = '<span class="hide">'+v.totalGiftsSent+'</span>'+v.totalGiftsSent;
					}
				}
			},
			handleError: function(aError) {},
			handleCompletion: function(aReason)
			{				
				$('.sendToFavouritesList','div#'+game+opt).html('<table cellpadding="0" cellspacing="0" border="0" class="display favouritesTable tablesorter" style="margin: 0 auto; margin-top: 15px;"></table>');
				$('.sendToNeighboursList','div#'+game+opt).html('<table cellpadding="0" cellspacing="0" border="0" class="display neighborsTable tablesorter" style="margin: 0 auto; margin-top: 15px;"></table>');
				
				
				$('.neighborsTable', 'div#'+game+opt).dataTable( {
					"aaData": data.norArr,
					"aoColumns": [
						{ "sTitle": bkP.getMsg('ToggleFavourite'), "bSortable": false },
						{ "sTitle": bkP.getMsg('Name') },
						{ "sSortDataType": "dom-text", "sType": "numeric", "sTitle": bkP.getMsg('LastBonus') },
						{ "sSortDataType": "dom-text", "sType": "numeric", "sTitle": bkP.getMsg('LastGift') },
						{ "sSortDataType": "dom-text", "sType": "numeric", "sTitle": bkP.getMsg('LastGiftSent') },
						{ "sSortDataType": "dom-text", "sType": "numeric", "sTitle": bkP.getMsg('TotalBonuses') },
						{ "sSortDataType": "dom-text", "sType": "numeric", "sTitle": bkP.getMsg('TotalGifts') },
						{ "sSortDataType": "dom-text", "sType": "numeric", "sTitle": bkP.getMsg('TotalGiftsSent') },
						{ "sTitle": bkP.getMsg('Select'), "bSortable": false }
					],
					"aaSorting": [[ 1, "asc" ]],
					"iDisplayLength": 100,
					"oSearch": {"sSearch": ""},
					"bJQueryUI": true,
					"aLengthMenu": [[100, -1], [100, bkP.getMsg('All')]],
					"sPaginationType": "full_numbers"
				});
				
				$('.favouritesTable', 'div#'+game+opt).dataTable( {
					"aaData": data.favArr,
					"aoColumns": [
						{ "sTitle": bkP.getMsg('ToggleFavourite'), "bSortable": false },
						{ "sTitle": bkP.getMsg('Name') },
						{ "sSortDataType": "dom-text", "sType": "numeric", "sTitle": bkP.getMsg('LastBonus') },
						{ "sSortDataType": "dom-text", "sType": "numeric", "sTitle": bkP.getMsg('LastGift') },
						{ "sSortDataType": "dom-text", "sType": "numeric", "sTitle": bkP.getMsg('LastGiftSent') },
						{ "sSortDataType": "dom-text", "sType": "numeric", "sTitle": bkP.getMsg('TotalBonuses') },
						{ "sSortDataType": "dom-text", "sType": "numeric", "sTitle": bkP.getMsg('TotalGifts') },
						{ "sSortDataType": "dom-text", "sType": "numeric", "sTitle": bkP.getMsg('TotalGiftsSent') },
						{ "sTitle": bkP.getMsg('Select'), "bSortable": false }
					],
					"aaSorting": [[ 1, "asc" ]],
					"iDisplayLength": 100,
					"oSearch": {"sSearch": ""},
					"bJQueryUI": true,
					"aLengthMenu": [[100, -1], [100, bkP.getMsg('All')]],
					"sPaginationType": "full_numbers"
				});
				
				
				$('.sendToNeighboursList','div#'+game+opt).parent().find('.hide').not('td > span').removeClass('hide');
				$('#'+game+'SendFreeGifts > .submenu > button').removeClass('hide');
				$('.sendToNeighboursList','div#'+game+opt).children('h3').remove();
				$('.sendToNeighboursList','div#'+game+opt).children('table').removeClass('hide');
			}
		});		
	}
	
	function loadBonuses(gID)
	{
		if(typeof(gID) != 'undefined')
		{
			var whereQry = " where gameID = '"+gID+"'";
			var andQry = " and gameID = '"+gID+"'";
		}
		else
		{
			var whereQry = '';
			var andQry = '';
		}
	
		if(bkP.options.deleteHistoryOlderThan != 0)
		{
			var now = Math.floor(new Date().getTime()/1000);
			var newerThan = now-bkP.options.deleteHistoryOlderThan;
			
			bkP.database.db.createStatement("DELETE FROM bonuses where status = '1' and time < '"+newerThan+"' ").executeAsync();
			bkP.database.db.createStatement("DELETE FROM requests where status = '1' and time < '"+newerThan+"' ").executeAsync();
			bkP.database.db.createStatement("DELETE FROM freegifts where time < '"+newerThan+"' ").executeAsync();
		}
		
		if(bkP.options.deleteOlderThan != 0)
		{
			var now = Math.floor(new Date().getTime()/1000);
			var newerThan = now-bkP.options.deleteOlderThan;

			bkP.database.db.createStatement("DELETE FROM bonuses where status = '0' and time < '"+newerThan+"' ").executeAsync();
		}
		
		
		var stmt = bkP.database.db.createStatement("SELECT * FROM bonuses where status = '0' "+andQry +" order by time DESC");
		stmt.executeAsync({
			htmls: {},
			htmlsManual: {},
			handleResult: function(aResultSet)
			{
				for (var row = aResultSet.getNextRow();	 row;	 row = aResultSet.getNextRow()) 
				{
					var v = 
					{
						id: 			row.getResultByIndex(0),
						gameID: 		row.getResultByIndex(1),
						status:			row.getResultByIndex(2),
						error:			row.getResultByIndex(3),
						title:			row.getResultByIndex(4),
						text:			row.getResultByIndex(5),
						image:			row.getResultByIndex(6),
						url:			row.getResultByIndex(7),
						time:			row.getResultByIndex(8),
						feedback:		row.getResultByIndex(9),
						link_data:		row.getResultByIndex(10),
						like_bonus:		row.getResultByIndex(11),
						comment_bonus:	row.getResultByIndex(12),
						resend_gift:	row.getResultByIndex(13),
						error_text:		row.getResultByIndex(14),
					}
					
					var gameID = v.gameID;					
					
					if(!bkP.options.games[gameID].enabled)
					{
						continue;
					}

					if(typeof(this.htmls[gameID]) == 'undefined')
					{
						this.htmls[gameID] = '';
						this.htmlsManual[gameID] = '';
					}
					
					
					if(checkIfBonusIsManual(gameID, v.title, v.text))
					{
						this.htmlsManual[gameID] += getNewManualBonusRow(gameID, v);
					}
					else
					{		
						this.htmls[gameID] += getNewBonusRow(gameID, v);
					}
				}
			},
			handleError: function(aError) {},
			handleCompletion: function(aReason)
			{
				for(var tmp in this.htmls)
				{
					var game = bkP.gamesData[tmp].systemName;
					$('div#'+game+'ManualBonusesList').prepend(this.htmlsManual[tmp]);
					$('div#'+game+'BonusesPendingList').prepend(this.htmls[tmp]);
				}
				updateCount();
				updateLoaded();
			}
		});
		
		var stmt = bkP.database.db.createStatement("SELECT * FROM requests where status = '0' "+andQry +" order by time DESC");
		stmt.executeAsync({
			htmls: {},
			htmlsManual: {},
			handleResult: function(aResultSet)
			{
				for (var row = aResultSet.getNextRow();	 row;	 row = aResultSet.getNextRow()) 
				{
					var v = 
					{
						id: 			row.getResultByIndex(0),
						gameID: 		row.getResultByIndex(1),
						status:			row.getResultByIndex(2),
						error:			row.getResultByIndex(3),
						title:			row.getResultByIndex(4),
						text:			row.getResultByIndex(5),
						image:			row.getResultByIndex(6),
						post:			row.getResultByIndex(7),
						time:			row.getResultByIndex(8),
						resend_gift:	row.getResultByIndex(9),
						error_text:		row.getResultByIndex(10),
					}
				
					var gameID = v.gameID;

					if(!bkP.options.games[gameID].enabled)
					{
						continue;
					}

					if(typeof(this.htmls[gameID]) == 'undefined')
					{
						this.htmls[gameID] = '';
						this.htmlsManual[gameID] = '';
					}

					if(checkIfRequestIsManual(gameID, v.text))
					{
						this.htmlsManual[gameID] += getNewManualRequestRow(gameID, v);
					}
					else
					{		
						this.htmls[gameID] += getNewRequestRow(gameID, v);
					}
				}
			},
			handleError: function(aError) {},
			handleCompletion: function(aReason)
			{
				for(var tmp in this.htmls)
				{
					var game = bkP.gamesData[tmp].systemName;
					$('div#'+game+'ManualBonusesList').prepend(this.htmlsManual[tmp]);
					$('div#'+game+'RequestsPendingList').prepend(this.htmls[tmp]);
				}
				updateCount();
				updateLoaded();
			}
		});
		
		var stmt = bkP.database.db.createStatement("SELECT * FROM bonuses where status = '1' "+andQry +" order by time DESC");
		stmt.executeAsync({
			htmls: {},
			htmlsError: {},
			handleResult: function(aResultSet)
			{
				for (var row = aResultSet.getNextRow();	 row;	 row = aResultSet.getNextRow()) 
				{
					var v = 
					{
						id: 			row.getResultByIndex(0),
						gameID: 		row.getResultByIndex(1),
						status:			row.getResultByIndex(2),
						error:			row.getResultByIndex(3),
						title:			row.getResultByIndex(4),
						text:			row.getResultByIndex(5),
						image:			row.getResultByIndex(6),
						url:			row.getResultByIndex(7),
						time:			row.getResultByIndex(8),
						feedback:		row.getResultByIndex(9),
						link_data:		row.getResultByIndex(10),
						like_bonus:		row.getResultByIndex(11),
						comment_bonus:	row.getResultByIndex(12),
						resend_gift:	row.getResultByIndex(13),
						error_text:		row.getResultByIndex(14),
					}
				
					var gameID = v.gameID;

					if(!bkP.options.games[gameID].enabled)
					{
						continue;
					}
					
					if(typeof(this.htmls[gameID]) == 'undefined')
					{
						this.htmls[gameID] = '';
						this.htmlsError[gameID] = '';
					}

					var tmp = getBonusHistoryRow(v);
					
					if(tmp.indexOf('noErrorClass') != -1)
						this.htmls[gameID] += tmp;
					else
						this.htmlsError[gameID] += tmp;
				}
			},
			handleError: function(aError) {},
			handleCompletion: function(aReason)
			{
				for(var tmp in this.htmls)
				{
					var game = bkP.gamesData[tmp].systemName;
					
					$('div#'+game+'BonusesHistoryList').prepend(this.htmls[tmp]);
					$('div#'+game+'FailedBonusesList').prepend(this.htmlsError[tmp]);	
					
					$('div#'+game+'FailedBonusesList').children('div.processManualBonusClick').css('cursor', 'pointer !important').attr('title', 'Click to manually receive');
					
					$('div#'+game+'BonusesHistoryList').children('div.noErrorClass').find('.bonusError').css('height', '23px');								
					$('div#'+game+'BonusesHistoryList').children('div.noErrorClass').removeClass('noErrorClass');
				}
				updateCount();
				updateLoaded();
			}
		});
		
		
		var stmt = bkP.database.db.createStatement("SELECT * FROM requests where status = '1' "+andQry +" order by time DESC");
		stmt.executeAsync({
			htmls: {},
			htmlsError: {},
			handleResult: function(aResultSet)
			{
				for (var row = aResultSet.getNextRow();	 row;	 row = aResultSet.getNextRow()) 
				{
					var v = 
					{
						id: 			row.getResultByIndex(0),
						gameID: 		row.getResultByIndex(1),
						status:			row.getResultByIndex(2),
						error:			row.getResultByIndex(3),
						title:			row.getResultByIndex(4),
						text:			row.getResultByIndex(5),
						image:			row.getResultByIndex(6),
						post:			row.getResultByIndex(7),
						time:			row.getResultByIndex(8),
						resend_gift:	row.getResultByIndex(9),
						error_text:		row.getResultByIndex(10),
					}
				
					var gameID = v.gameID;

					if(!bkP.options.games[gameID].enabled)
					{
						continue;
					}
					
					if(typeof(this.htmls[gameID]) == 'undefined')
					{
						this.htmls[gameID] = '';
						this.htmlsError[gameID] = '';
					}

					var tmp = getRequestHistoryRow(v);
					
					if(tmp.indexOf('noErrorClass') != -1)
						this.htmls[gameID] += tmp;
					else
						this.htmlsError[gameID] += tmp;
				}
			},
			handleError: function(aError) {},
			handleCompletion: function(aReason)
			{
				for(var tmp in this.htmls)
				{
					var game = bkP.gamesData[tmp].systemName;
					
					$('div#'+game+'RequestsHistoryList').prepend(this.htmls[tmp]);
					$('div#'+game+'FailedGiftsList').prepend(this.htmlsError[tmp]);
					
					$('div#'+game+'FailedGiftsList').children('div.processManualRequestClick').css('cursor', 'pointer !important').attr('title', 'Click to manually receive');
					
					$('div#'+game+'RequestsHistoryList').children('div.noErrorClass').find('.bonusError').css('height', '23px');
					$('div#'+game+'RequestsHistoryList').children('div.noErrorClass').removeClass('noErrorClass');
				}
				updateCount();
				updateLoaded();
			}
		});

		var stmt = bkP.database.db.createStatement("SELECT * FROM freegifts "+whereQry +" order by time DESC");
		stmt.executeAsync({
			htmls: {},
			handleResult: function(aResultSet)
			{
				for (var row = aResultSet.getNextRow();	 row;	 row = aResultSet.getNextRow()) 
				{
					var v = 
					{
						id: 			row.getResultByIndex(0),
						gameID: 		row.getResultByIndex(1),
						friend:			row.getResultByIndex(2),
						gift:			row.getResultByIndex(3),
						time:			row.getResultByIndex(4),
						is_thank_you:	row.getResultByIndex(5),
					}
				
					var gameID = v.gameID;

					if(!bkP.options.games[gameID].enabled)
					{
						continue;
					}
					
					if(typeof(this.htmls[gameID]) == 'undefined')
					{
						this.htmls[gameID] = '';
					}
					
					this.htmls[gameID] += getGiftHistoryRow(gameID, v, v.is_thank_you);
				}
			},
			handleError: function(aError) {},
			handleCompletion: function(aReason)
			{
				for(var tmp in this.htmls)
				{
					var game = bkP.gamesData[tmp].systemName;
					
					$('div#'+game+'SendFreeGiftsHistoryList').prepend(this.htmls[tmp]);
				}
				updateCount();
				updateLoaded();
			}
		});
	}